﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Net_SCADA
{
    public partial class fdMainZone : UserControl
    {

        private string _unit = "℃";
        private string _title = "";
        private int _v1id = 0;
        private int _v2id = 0;
        private int _v3id = 0;
        private int _v4id = 0;
        private string _v1 = "9";
        private string _v2 = "10";
        private string _v3 = "5";
        private string _v4 = "6";
        private bool _isZoneAlarm = false;
        public List<int> AlarmID { get; set; } = new List<int>();
        public int ZoneID { get; set; }
        public string Unit { get { return _unit; } set { _unit = value; showTitle(); } }
        public string Title { get { return _title; } set { _title = value; showTitle(); } }
        public int V1ID { get { return _v1id; } set { lbl_Value1.Visible = value != 0; _v1id = value; } }
        public int V2ID { get { return _v2id; } set { lbl_Value2.Visible = value != 0; _v2id = value; } }
        public int V3ID { get { return _v3id; } set { lbl_Value3.Visible = value != 0; _v3id = value; } }
        public int V4ID { get { return _v4id; } set { lbl_Value4.Visible = value != 0; _v4id = value; } }
        public bool IsZoneAlarm
        {
            get { return _isZoneAlarm; }
            set
            {
                if (_isZoneAlarm != value)
                {
                    if (value)
                    {
                        this.BackColor = lbl_Value1.BackColor = lbl_Value2.BackColor = lbl_Value3.BackColor = lbl_Value4.BackColor = Color.DarkRed;
                        lbl_Value1.ForeColor = lbl_Value2.ForeColor = Color.Yellow;
                        lbl_Value3.ForeColor = lbl_Value4.ForeColor = Color.WhiteSmoke;
                    }
                    else
                    {
                        this.BackColor = lbl_Value2.BackColor = lbl_Value1.BackColor = lbl_Value3.BackColor = lbl_Value4.BackColor = Color.WhiteSmoke;
                        lbl_Value1.ForeColor = Color.DarkGreen;
                        lbl_Value2.ForeColor = Color.Tomato;
                        lbl_Value3.ForeColor = lbl_Value4.ForeColor = Color.Gray;
                    }
                    _isZoneAlarm = value;
                }
            }
        }
        public fdMainZone()
        {
            InitializeComponent();
        }
        public fdMainZone(string title, string unit)
        {
            InitializeComponent();
            lbl_Name.Text = title + " " + unit;
        }
        private void showTitle()
        {
            lbl_Name.Text = _title + " " + _unit;
        }
        private void fdMainZone_Resize(object sender, EventArgs e)
        {
            reSetInterface();
        }

        public void reFlashValue()
        {
            lbl_Value3.Text = "-" + _v3;// string.Format("{0:#0.##}", Convert.ToDouble(_v1.ToString()));
            lbl_Value4.Text = "+" + _v4;
            lbl_Value1.Text = _v1;// string.Format("{0:#0.##}", Convert.ToDouble(_v1.ToString()));
            lbl_Value2.Text = _v2;
        }
        public void reFlashValue(string v1, string v2,string v3, string v4)
        {
            _v1 = v1;
            _v2 = v2;
            _v3 = v3;
            _v4 = v4;
            reFlashValue();
        }

        private void reSetInterface()
        {
            int w = this.Width/10;
            this.Width = w * 10;
            int h = this.Height / 11;
            this.Height = h * 11;
            lbl_Name.Location = new Point(0, 0);
            lbl_Name.Size = new Size(w, 3 * h);
            lbl_Value2.Size = lbl_Value1.Size = new Size(6 * w, 4 * h);

            lbl_Value1.Location = new Point(4*w, 3 * h);
            
            lbl_Value1.Font = Getfont(lbl_Value1.Height, "Arial", FontStyle.Bold);
            lbl_Value1.TextAlign = ContentAlignment.MiddleRight;

            lbl_Value2.Location = new Point(4*w, 7 * h);
            lbl_Value2.Font = Getfont(lbl_Value2.Height-5, "Arial", FontStyle.Bold);
            lbl_Value2.TextAlign = ContentAlignment.BottomRight;

            lbl_Value3.Location = new Point(0, 3 * h);
            lbl_Value4.Location = new Point(0, 7 * h);
            lbl_Value3.Size = lbl_Value4.Size = new Size(4 * w, 4 * h);
            lbl_Value3.Font = lbl_Value4.Font = Getfont(lbl_Value3.Height-8, "Arial", FontStyle.Regular);


        }
        private static Font Getfont(int height, string fonttype, FontStyle fontStyle)
        {
            Label alabel = new Label();
            SizeF sizeF;
            string str = "1234";
            float fontsize = 5f;
            Font font;
            Graphics g = alabel.CreateGraphics();
            g.PageUnit = GraphicsUnit.Pixel;
            while (true)
            {
                font = new Font(fonttype, fontsize, fontStyle);// new Font("Arial", fontsize, FontStyle.Bold);
                sizeF = g.MeasureString(str, font);
                if (sizeF.Height >= height)
                {
                    return font;
                }
                fontsize += 0.5f;
            }
        }

        private void fdMainZone_Load(object sender, EventArgs e)
        {
            reSetInterface();
            showTitle();
            reFlashValue();
        }
    }
}
